package com.bluesky.plum.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.bluesky.plum.domain.northwind.Category;
import com.bluesky.plum.domain.northwind.Customer;
import com.bluesky.plum.domain.northwind.Employee;
import com.bluesky.plum.domain.northwind.Order;
import com.bluesky.plum.domain.northwind.OrderDetail;
import com.bluesky.plum.domain.northwind.Product;
import com.bluesky.plum.domain.northwind.Shipper;
import com.bluesky.plum.domain.northwind.Supplier;
import com.bluesky.plum.domain.repository.Repository;
import com.bluesky.plum.domain.repository.RepositoryFactory;
import com.bluesky.plum.domain.repository.impl.HibernateRepository;
import com.bluesky.plum.domain.repository.impl.HibernateRepositorySupport;
import com.bluesky.plum.domain.tool.Calculator;
import com.bluesky.plum.domain.tool.CalculatorImpl;
import com.bluesky.plum.uimodels.standard.BusinessObject;

public class SpringFrameworkUtil {

	/**
	 * get the current Spring WebApplicationContext, which is initialized by a
	 * Servlet listener.
	 * 
	 * @return
	 */
	public static ApplicationContext getWebApplicationContext(ServletContext sc) {
		return WebApplicationContextUtils.getWebApplicationContext(sc);
	}

	private static ApplicationContext applicationContext;

	private static void loadSpringAPplicationContext() {
		applicationContext = new ClassPathXmlApplicationContext(
				new String[] { "config/spring/spring-config.xml" });

	}

	/**
	 * if it is not a web application, this method will initialize the context
	 * 
	 * @param paths
	 * @return
	 */
	public static ApplicationContext getApplicationContext() {
		if (applicationContext == null)
			loadSpringAPplicationContext();

		return applicationContext;
	}

	public static void registerRepositories(
			ApplicationContext applicationContext) {

		// get the support bean from Spring IOC
		HibernateRepositorySupport hibernateRepositorySupport = (HibernateRepositorySupport) applicationContext
				.getBean("hibernateRepositorySupport");

		// create the repositories
		RepositoryFactory.register(Category.class, new HibernateRepository(
				hibernateRepositorySupport, Category.class));
		RepositoryFactory.register(Customer.class, new HibernateRepository(
				hibernateRepositorySupport, Customer.class));
		RepositoryFactory.register(Employee.class, new HibernateRepository(
				hibernateRepositorySupport, Employee.class));
		RepositoryFactory.register(Order.class, new HibernateRepository(
				hibernateRepositorySupport, Order.class));
		RepositoryFactory.register(OrderDetail.class, new HibernateRepository(
				hibernateRepositorySupport, OrderDetail.class));
		RepositoryFactory.register(Product.class, new HibernateRepository(
				hibernateRepositorySupport, Product.class));
		RepositoryFactory.register(Shipper.class, new HibernateRepository(
				hibernateRepositorySupport, Shipper.class));
		RepositoryFactory.register(Supplier.class, new HibernateRepository(
				hibernateRepositorySupport, Supplier.class));

	}

	public static BusinessObject[] getBusinessObjectPairs(
			ApplicationContext applicationContext) {
		List<BusinessObject> businessObjects = new ArrayList<BusinessObject>();

		// 3 business service objects

		Calculator calc = new CalculatorImpl();
		businessObjects.add(new BusinessObject(Calculator.class, calc));

		// HRService hrsvc = new HRService();
		// businessObjects.put(HRService.class, hrsvc);
		//
		// PersonRepository repo = new PersonRepository();
		// businessObjects.put(PersonRepository.class, repo);
		//
		Repository categoryRepo = (Repository) RepositoryFactory
				.getRepository(Category.class);
		Repository customerRepo = (Repository) RepositoryFactory
				.getRepository(Customer.class);
		Repository employeeRepo = (Repository) RepositoryFactory
				.getRepository(Employee.class);
		Repository orderRepo = (Repository) RepositoryFactory
				.getRepository(Order.class);
		// Repository orderDetailRepo = (Repository)
		// RepositoryFactory.getRepository(Employee.class);
		Repository productRepo = (Repository) RepositoryFactory
				.getRepository(Product.class);
		Repository shipperRepo = (Repository) RepositoryFactory
				.getRepository(Shipper.class);
		Repository supplierRepo = (Repository) RepositoryFactory
				.getRepository(Supplier.class);

		businessObjects.add(new BusinessObject(Repository.class, "T",
				Category.class, categoryRepo));
		businessObjects.add(new BusinessObject(Repository.class, "T",
				Customer.class, customerRepo));
		businessObjects.add(new BusinessObject(Repository.class, "T",
				Employee.class, employeeRepo));
		businessObjects.add(new BusinessObject(Repository.class, "T",
				Order.class, orderRepo));
		businessObjects.add(new BusinessObject(Repository.class, "T",
				Product.class, productRepo));
		businessObjects.add(new BusinessObject(Repository.class, "T",
				Shipper.class, shipperRepo));
		businessObjects.add(new BusinessObject(Repository.class, "T",
				Supplier.class, supplierRepo));

		//
		// HibernateRepository<Employee> hibEmployeeRepo = new
		// HibernateRepository<Employee>(
		// Employee.class);
		// businessObjects.put(EmployeeRepository.class, employeeRepo);

		BusinessObject[] bosarray = businessObjects
				.toArray(new BusinessObject[] {});

		return bosarray;
	}

}
